package com.yulanda.web.controller.sys;

import com.yulanda.common.annotation.DataSource;
import com.yulanda.common.core.controller.BaseController;
import com.yulanda.common.core.domain.R;
import com.yulanda.common.enums.DataSourceType;
import com.yulanda.system.domain.SysUser;
import com.yulanda.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "多数据源测试",tags = "多数据源测试")
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    protected final Logger logger = LoggerFactory.getLogger(SysUserController.class);

    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 主库
     * @return
     */
    @ApiOperation(value = "多数据源主库api", notes = "请求主库master")
    @GetMapping("/master")
    @DataSource(DataSourceType.MASTER)
    public R master()
    {
        List<SysUser> list = iSysUserService.list();
        logger.info("---MASTER----查询成功");
        return R.success(list);
    }

    /**
     * 从库
     * @return
     */
    @ApiOperation(value = "多数据源从库api", notes = "请求从库slave")
    @GetMapping("/slave")
    @DataSource(DataSourceType.SLAVE)
    public R slave()
    {
        List<SysUser> list = iSysUserService.list();
        logger.info("---slave----查询成功");
        return R.success(list);
    }
}
